package Chat;


import java.io.IOException;

import javax.swing.JOptionPane;


/**
 * Controls the connection to the server.
 * It holds functions for communication to be used 
 * by the control classes.
 */
public class ClientConsole implements ChatIF 
{
	//Class variables *************************************************
	
	/**
	* The default port to connect on.
	*/
	final public static int DEFAULT_PORT = 2222;
	
	//Instance variables **********************************************
	
	/**
	* The instance of the client that created this ConsoleChat.
	*/
	public ChatClient cclient;
	/** The flag that indicates if an answer was received from the server.
	 * when this flag turns true, a control class is allowed to read the result
	 */
	private static boolean answerReceivedFlag;
	/** holds a static reference to the user name. */
	private static String username;
	/** holds the result object sent from the server */
	private static Object result = null;
	
	//Constructors ****************************************************
	
	/**
	* Constructs an instance of the ClientConsole UI.
	*
	* @param host The host to connect to.
	* @param port The port to connect on.
	*/
	public ClientConsole(String host, int port) 
	{
		
		try 
	 {
	   cclient= new ChatClient(host, port, this);
	 //  answerReceivedFlag = false;
	 }//try 
	 catch(IOException exception) 
	 {
	   JOptionPane.showMessageDialog(null,"Error: Can't setup connection to server!\n" +
	   		"please check the server IP and port and try again.\n" +
	   		"for any further help, please contact your administrator.","CCRM Error",JOptionPane.ERROR_MESSAGE);
	 }//catch
	}//Client
	
	
	//Instance methods ************************************************
	
	/**
	* This method waits for input .Once it is 
	* received, it sends it to the client's message handler.
	*/
	public void accept(Object message) 
	{
		result = null;//reset result for another message
		answerReceivedFlag = false;//it means message is empty now so there is no answer to take yet
		cclient.handleMessageFromClientUI(message);
	}//accept
	
	/**
	* This method overrides the method in the ChatIF interface.  It
	* displays a message onto the screen.
	*
	* @param string sent from server.
	*/
	public void display(Object message){
		receiveAnswer(message);
	}
	
	/**
	* This method receives an object and sets the result.
	*
	* @param object sent from server.
	*/
	public void receiveAnswer(Object message) 
	{
		setResultobject(message);
	}//receive answer
	
	/**
	* This method sets the result received from server 
	* @param object received from server.
	*/
	public void setResultobject(Object o){
		 result = o;
		 answerReceivedFlag = true;
	}
	
	/**
	* This method gets the result received from server 
	* @param object received from server.
	*/
	public Object getResultobject()
	{
		  
		Object temp;
		temp = result;
		result = null;//reset result for another message
		answerReceivedFlag = false;//it means message is empty now so there is no answer to take
		return temp;
	}//getRs   
	
	/*********************************************************getRs***********************************/
	
	public boolean getAnswerReceivedFlag()
	{
	  return answerReceivedFlag;
	}
	public void setAnswerReceivedFlag(boolean b)
	{
	  answerReceivedFlag = b;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String user) {
		username = user;
	}
}
